package vek.GUI.SearchAndListView;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import vek.GUI.Infoview.KeyBoardButton;
import vek.ObjectDatabase.Search.SearchType;
import vek.controllers.SearchAndList.KeyboardSubPanelController;
import vek.controllers.SearchAndList.KeyboardMainController;
import vek.controllers.SearchAndList.NavigationPanelController;
/**
 * This class contains the JComponents which makes up the KeyboardView and some limited functionality, 
 * which allows the program to get information about what the user has "typed" on the keyboard.
 * 
 * @author Christian, Michal og Jakob (Hold 1, gruppe 2)
 */

public class KeyboardView extends JPanel{
    
    private NavigationPanelController navigationPanelController;
    private KeyboardMainController maincontroller;
    private Dimension sizeOfKeyboardButton;
    private JTextArea textField;
    private JPanel keyboardPanel;
    private GridLayout keyboardLayout;
    private KeyboardSubPanelController controller;
    private String[] keyboard = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "?",  
                                  "Q", "W", "E", "R", "T", "Y", "U", "I", "O","P", "Å", 
                                  "A", "S", "D", "F", "G", "H", "J", "K",
                                  "L", "Æ", "Ø", "Z", "X", "C", "V", "B", "N",
                                  "M" }; // sets the keyboard layout of the keyboard in the KeyBoardView
    private JButton doneButton;
    private JButton backspaceButton; // the backspace button which deleted a character
    private SearchType currentSearchType;
    private ArrayList<String> savedTextFields;
    private ArrayList<SearchType> keyboards;
    
    /**
     * Creates the KeyboardView and its content.
     * @param size the size of this component
     * @param npc the NavigationPanelController which handles input in the search panel (botton panel)
     * @param kpmc the KeyboardMainController which handles input when the buttons on the on-screen keyboard is pressed
     */
    
    public KeyboardView(Dimension size, NavigationPanelController npc, KeyboardMainController kpmc)
    {
        navigationPanelController = npc;
        
        // new objects
        textField = new JTextArea(1, 1);
        keyboardPanel = new JPanel();
        
        textField.setPreferredSize(new Dimension((int)size.getWidth(), (int)(size.getHeight() * 0.15)));
        keyboardPanel.setPreferredSize(new Dimension((int)(size.getWidth() * 0.9), (int)(size.getHeight() * 0.85)));
        
        //sizeOfKeyboardButton = new Dimension((int) (keyboardPanel.getPreferredSize().getWidth() * 0.04),(int) (keyboardPanel.getPreferredSize().getHeight() * 0.15));
        sizeOfKeyboardButton = new Dimension(25,25);
        maincontroller = kpmc;
        
        savedTextFields = new ArrayList<String>();
        keyboards = new ArrayList<SearchType>();
        
        setVisible(false);
        setPreferredSize(size);
 
        createButtons();
        
        //states
        
        
        //methods
        createLayout();
        
        addContent();
    }
    
    /**
     * Gets the amount of characters (including non-visible characters) in the text field which shows what has been typed.
     * @return the amount of characters in the text field
     */
    
    public int getTextFieldLength()
    {
        return textField.getText().length();
    }
    
    /**
     * Creates the buttons, sets the name and adds ActionListeners
     */
    
    public void createButtons()
    {
        backspaceButton = new JButton("<-");
        backspaceButton.setName("backspaceButton");
        backspaceButton.addActionListener(maincontroller);
        doneButton = new JButton("Færdig");
        doneButton.setName("doneButton");
        doneButton.addActionListener(maincontroller);
    }
    
    /**
     * Hides the keyboard.
     */
    
    public void hideKeyBoard()
    {
        clearTextField();
        setVisible(false);
        revalidate();
    }
    
    /**
     * Shows the keyboard. Gets the previous text written for this specific search function by using the method getSavedTextField.
     * @param type the search type.
     */
   
    
    public void showKeyboard(SearchType type)
    {
        String savedTextField = getSavedTextField(type);
        if( savedTextField != null )
        {
            textField.setText(savedTextField);
        }
        currentSearchType = type;
        controller.changeType(type);
        setVisible(true);
        revalidate();
        
    }
    
    /**
     * Gets the text field, that is the String which has previously been typed in a now-hidden keyboardpanel.
     * This method makes sure that when the user leaves a keyboardpanel and reenters it, the text the user previously entered is still there.
     * @param type the type of keyboard panel, each type saves its own text field
     * @return the saved text field as a String
     */
    
    public String getSavedTextField(SearchType type)
    {
        if(keyboards.contains(type) == false)
        {
            return null;
        }
        else
        {
            System.out.println("keyboards index: " + keyboards.indexOf(type) );
            return savedTextFields.get(keyboards.indexOf(type));
        }
    }
    
    /**
     * gets the current search type for this keyboardpanel
     * @return the search type for this keyboardpanel
     */
    
    public SearchType getCurrentSearchType()
    {
        return currentSearchType;
    }
    
    /**
     * sets the controller for this component. This method is used to set the controller later than object creation (this is, in the constructor)
     * @param kpc the KeyBoardSubPanelController for this component which handles the input from the on-screen keyboard
     */
    
    public void setController(KeyboardSubPanelController kpc)
    {
        controller = kpc;
         createKeyboard(keyboard);
    }
    
    /**
     * Removes the last character from the text field, essentially doing the same as hitting backspace in a text editor.
     */
    
    public void removeLastCharFromTextField()
    {
        textField.setText(textField.getText().substring(0, textField.getText().length() - 1));
    }
    
    /**
     * sets the text field to be blank
     */
    
    public void clearTextField()
    {
        textField.setText("");
    }
    
    /**
     * adds a string at the end of the text field.
     * @param s the string which is to be added to the text field
     */
    
    public void appendToTextField(String s)
    {
        textField.append(s);
    }
    
    /**
     * Gets the text in the text field in this component
     * @return the text in the text field as a String
     */
    
    public String getTextFieldText()
    {
        return textField.getText();
    }
    
    /**
     * Adds the component to this component.
     * This method adds the text field, the keyboardpanel, the backspace and the done button to this component.
     */
      
    private void addContent()
    {
        add(textField, BorderLayout.NORTH);
        add(keyboardPanel, BorderLayout.CENTER);
        
        add(backspaceButton, BorderLayout.WEST);
        add(doneButton, BorderLayout.EAST);
        //addButtonsToKeyboardPanel();
    }
    
    /**
     * Saves the text field
     */
    
    public void addSavedTextField()
    {
        if( keyboards.contains(controller.getSearchType()) ) // if there is already a saved text field of this type then override it, only keeping this latest one
        {
            savedTextFields.set(keyboards.indexOf( controller.getSearchType() ), textField.getText());
        }
        else
        {
            keyboards.add( controller.getSearchType() );
            savedTextFields.add(textField.getText());
        }
        
    }

    /**
     * creates the layout for this component and for the keyboardPanel compnent.
     * This component uses the BorderLayout layout, the keyboardpanel uses the keyboardLayout (which is a GridLayout)
     */
    
    private void createLayout()
    {
       setLayout(new BorderLayout());
       keyboardLayout = new GridLayout(0, 11);
       keyboardPanel.setLayout(keyboardLayout);
    }
    
    /**
     * Creates the keyboard by adding KeyBoardButtons, uses the "keyboard" variable to determine the text displayed on the buttons.
     * @param s the layout of the keyboard starting at the top left corner and ending with the bottom right.
     */
    
    private void createKeyboard(String[] s)
    {
        for(int i = 0; i < s.length; i++)
        {
            KeyBoardButton button = new KeyBoardButton(s[i], sizeOfKeyboardButton);
            button.addActionListener(controller);
            keyboardPanel.add(button);
        }
    }

}
